package unipi.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import unipi.database.Tasks;
import unipi.database.User;
import unipi.helpers.HTMLPages;
import unipi.helpers.LoginCookie;
import unipi.helpers.Validator;

public class Calendar extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	public Calendar()
	{
		super();
	}
	
	public void init() throws ServletException
	{
		
	}
	private String getPage(String uid, int month, int year, String view, String userID)
	{
		String[] monthNames = {"Ιανουάριος", "Φεβρουάριος", "Μάρτιος", "Απρίλιος", "Μάιος", "Ιούνιος", "Ιούλιος",
						   "Αύγουστος", "Σεπτέμβριος", "Οκτώβριος", "Νοέμβριος", "Δεκέμβριος"};
		
		java.util.Calendar cal = java.util.Calendar.getInstance();
		if(month != 0)
			cal.set(java.util.Calendar.MONTH, month - 1);
		else
			month = cal.get(java.util.Calendar.MONTH) + 1;
		cal.set(java.util.Calendar.DAY_OF_MONTH, 1);	

		if (year != 0)
			cal.set(java.util.Calendar.YEAR, year);
		else
			year = cal.get(java.util.Calendar.YEAR);	
		
			
		int weekday = (cal.get(java.util.Calendar.DAY_OF_WEEK)) - 2;
		if(weekday < 0) weekday += 7;
		int maxday = cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);
		
		String content = "<h1>" + (view.equals("cal")?"Ημερολόγιο":"Ιστορικό")+ "</h1>";
		if(!uid.equals(userID))
		{
			Map<String, String> data = User.getUserData(userID);
			if(data != null)
				content += "<h2>του χρήστη " + data.get("surname") + " " + data.get("name") + "</h2>";			
		}
		content += "<h2>" + monthNames[month - 1] + " " + year + "</h2>";	
		
		content += "<form action=\"Calendar\" method=\"get\" name=\"fmchangemonth\"><div class=\"ddldivstyle\">" +
					"<select name=\"month\" onchange=\"document.fmchangemonth.submit();\" >";
		for(int x = 1; x <= monthNames.length; x++)
		{
			String selected = (x == month)?"selected=\"selected\"":"";
			content += "<option value=\"" + x + "\" " + selected + ">" + monthNames[x-1] + "</option>";			
		}
		content += "</select></div>";
		content += "<div class=\"ddldivstyle\"><select name=\"year\" onchange=\"document.fmchangemonth.submit();\" >";
		for(int x = year - 20; x <= year + 20; x++)
		{
			String selected = (x == year)?"selected=\"selected\"":"";
			content += "<option value=\"" + x + "\" " + selected + ">" + x + "</option>";			
		}
		content += "</select></div>";				
		content += "<input type=\"hidden\" name=\"view\" value=\"" + view + "\">";
		content += "<input type=\"hidden\" name=\"uid\" value=\"" + userID + "\">";
		content += "</form><br /><br />";
		
		Map<Integer, String[]> data = new HashMap<Integer, String[]>();
		List<List<String>> rawdata;
		
		if(view.equals("cal"))
			rawdata = Tasks.getCalendarTasks(userID, Integer.toString(month), Integer.toString(year));
		else
			rawdata = Tasks.getCalendarProgress(userID, Integer.toString(month), Integer.toString(year));
		
		if(rawdata != null)
		{
			for(List<String> row : rawdata)
			{
				data.put(Integer.parseInt(row.get(1)), row.get(0).split(","));
			}			
		}
		
		content += HTMLPages.getCalendarWeek(1, maxday, weekday, data);
		
		if(view.equals("cal"))
		{
			content+="<table class=\"memo\"><tr>" +
					"<td><img src=\"./images/calOK.png\" alt=\"\"/>Τρέχουσες</td>" +
					"<td><img src=\"./images/calNotStarted.png\" alt=\"\"/>Δεν έχουν αρχίσει</td>" +
					"<td><img src=\"./images/calShouldStarted.png\" alt=\"\"/>Πρέπει να αρχίσουν"+
					"<td><img src=\"./images/calLate.png\" alt=\"\"/>Αργοπορημένες</td>" +
					"</tr></table>" +
					"<div style=\"clear:both;\"></div>";
		}
		
		return HTMLPages.getGeneralPage(content, uid);	
	}
	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];
			
			request.setCharacterEncoding("utf-8");
			String monthP = request.getParameter("month");
			String yearP = request.getParameter("year");
			String view = request.getParameter("view");
			String userID = request.getParameter("uid");
			int month;
			int year;
			
			month = (monthP == null)?0:(Validator.isNumber(monthP))?Integer.parseInt(monthP):0;
			year = (yearP == null)?0:(Validator.isNumber(yearP))?Integer.parseInt(yearP):0;
			userID = (userID == null)?uid:(Validator.isNumber(userID)?userID:uid);
			if(view == null) view = "cal";
			
			response.setContentType("text/html; charset=UTF-8");	
			PrintWriter out = response.getWriter();
			
			out.println(getPage(uid, month, year, view, userID));
			
			out.close();			
		}
	}
	
}
